import time

from selenium.webdriver.common.by import By

from common.utils import BlogDriver

#博客登录
class BlogLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    #成功登录
    def LoginSucTest(self):
        #输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        #输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        #点击登陆
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)
        #查找主页信息，登陆成功
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")


    #异常登录
    def LoginFalTest(self):

        #用户名正确密码错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        time.sleep(1)#等待弹窗
        #切换弹窗模式
        alert=self.driver.switch_to.alert
        #关闭弹窗
        alert.accept()

        #获取登陆文本信息
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.container-login > div > h3").text
        #断言是否还处于登录界面
        assert text=="登陆"

        #清除用户名及密码信息
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        #用户名错误密码正确
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhang")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)  # 等待弹窗
        # 切换弹窗模式
        alert = self.driver.switch_to.alert
        # 关闭弹窗
        alert.accept()

        # 获取登陆文本信息
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3").text
        # 断言是否还处于登录界面
        assert text == "登陆"

        # 清除用户名及密码信息
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 用户名为空密码正确
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)  # 等待弹窗
        # 切换弹窗模式
        alert = self.driver.switch_to.alert
        # 关闭弹窗
        alert.accept()

        # 获取登陆文本信息
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3").text
        # 断言是否还处于登录界面
        assert text == "登陆"

